package com.tianyu.leyou.service.impl;

import com.tianyu.leyou.dao.UserDao;
import com.tianyu.leyou.model.UserBean;
import com.tianyu.leyou.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    UserDao userDao;

    @Override
    public Page list(Integer page, Integer size, UserBean user) {

        /*封装条件start*/
        UserBean paramUser = new UserBean();
        if (user.getUsername() != null && !"".equals(user.getUsername())) {
            paramUser.setUsername(user.getUsername());
        }

        Example<? extends UserBean> ex = Example.of(paramUser);
        /*封装条件end*/

        /*分页条件封装*/
        Pageable pageable = PageRequest.of(page, size);
        /*分页条件封装end*/

        Page<? extends UserBean> page1 = userDao.findAll(ex, pageable);
        return page1;
    }

    @Override
    public void add(UserBean user) {
     userDao.save(user);
    }

    @Override
    public void update(Integer id, UserBean user) {
        UserBean one = userDao.getOne(id); //检索单条记录的方法
        one.setPassword(user.getPassword());
        one.setUsername(user.getUsername());
        userDao.save(one); //修改数据的作用
        return;
    }

    @Override
    public void delete(Integer id) {
     userDao.deleteById(id);
    }

    @Override
    public UserBean login(String uname, String passwd) {

        return null;

    }
}

























